/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __LIB3D_MATERIAL_TEXTUREATLAS_H__
#define __LIB3D_MATERIAL_TEXTUREATLAS_H__

#include <SharedRep.h>
#include <memory>
#include <vector>
#include <Atom.h>
#include <math/Rect.h>

namespace liba { namespace filesystem { class String; }}

namespace liba {
namespace lib3d {

namespace hard
{
	class Hardware;
	class RenderTexture;
}

namespace material {

struct AtlasFrame
{
	Atom name;
	math::Rect <int> rect;
};

class TextureAtlasRep : public SharedPrecachedRepBase
{
public:
	TextureAtlasRep(const filesystem::String & name)
	{
		start_loading();
	}

	~TextureAtlasRep() {}

	void load(const filesystem::String & name);
	void render_to_list(hard::Hardware * hardware, hard::RenderTexture * render_texture);
	
	int GetWidth(bool forceLoad = false);
	int GetHeight(bool forceLoad = false);

	const AtlasFrame& GetFrame(size_t index) const;
	const AtlasFrame& GetFrame(const Atom &name) const;

private:
	std::auto_ptr <class TextureSimple> texture;
	std::vector <AtlasFrame> frames;
};

class TextureAtlas : public SharedPrecached<filesystem::String, TextureAtlasRep>
{
public:
	typedef name_type Name;
	TextureAtlas(const filesystem::String & name)
	{
		load_rep(name);
	}

	virtual TextureAtlas * clone()const;

	void render_to_list(hard::Hardware * hardware, hard::RenderTexture * render_texture)
	{
		get_rep()->render_to_list(hardware, render_texture);
	}
};


} // namespace material
} // namespace lib3d
} // namespace liba
using namespace liba;

#endif //__LIB3D_MATERIAL_TEXTUREATLAS_H__